/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elementsDefense.towers;

import elementsDefense.towers.projectils.Canonball;
import com.jme3.asset.AssetManager;
import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import elementsDefense.enemies.BasicEnemy;

/**
 *
 * @author Ricardo
 */
public class BasicTower extends Tower{
    ParticleEmitter fire;
    static Material matTower;
    
    public void build(AssetManager assetManager, Node node, Vector3f location){
        super.build(assetManager, node, location);
        if(matTower==null){
            matTower = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
            matTower.setTexture("ColorMap", assetManager.loadTexture("Textures/tower.jpg"));
        }
        geom = assetManager.loadModel("Models/tower.j3o");
//        geom.rotate(0, FastMath.HALF_PI, 0);
        geom.setMaterial(matTower);
        node.attachChild(geom);
        this.fireLocation = new Vector3f(location).add(0, 0.77f, 0);
        geom.setLocalTranslation(location);
        
        searchAndDestroy = new SearchAndDestroyControl(2, 1f, 0.1f, this);
        geom.addControl(searchAndDestroy);
        
        
        fire = new ParticleEmitter("Emitter", ParticleMesh.Type.Triangle, 6);
        Material mat_red = new Material(assetManager, 
                "Common/MatDefs/Misc/Particle.j3md");
        mat_red.setTexture("Texture", assetManager.loadTexture(
                "Textures/flame.png"));
        fire.setMaterial(mat_red);
        fire.setImagesX(2); 
        fire.setImagesY(2); // 2x2 texture animation
        fire.setEndColor(  new ColorRGBA(1f, 0f, 0f, 1f));   // red
        fire.setStartColor(new ColorRGBA(1f, 1f, 0f, 0.5f)); // yellow
        fire.setStartSize(0.2f);
        fire.setEndSize(0.01f);
        fire.setGravity(0, 0, 0);
        fire.setLowLife(0.4f);
        fire.setRotateSpeed(0.4f);
        fire.setHighLife(0.5f);
        fire.setParticlesPerSec(0);
        fire.getParticleInfluencer().setVelocityVariation(0.3f);
        node.attachChild(fire);
    }

    boolean fireEffects = false;
    void prepareToFire(BasicEnemy enemy, float animPhase){
        if(!fireEffects){
            fireEffects=true;
            fire.getParticleInfluencer().setInitialVelocity(enemy.getLocation().subtract(fireLocation).normalize().mult(3));
            fire.setLocalTranslation(fireLocation);
            fire.emitAllParticles();
        }
        Vector3f look = enemy.getLocation();
        look.y=0;
        geom.lookAt(look, Vector3f.UNIT_Y);
    }
    
    void fire(BasicEnemy enemy){
        //fire projectil
        new Canonball(assetManager, node, this).shootAt(enemy);
        fireEffects = false;
    }

    @Override
    int getCost() {
        return 50;
    }
  
}
